using UnityEngine;
using com.shinian.fcg.fbs;
using FCGGameConfig = com.shinian.fcg.fbs.GameConfig;
using FCG.cache.proxy_model;
using System.Collections.Generic;
using GF.Debug;
using GF.ResourcesLoad;

//获取图文的方法集Image&Text的Finder
public class ITFinder : MonoBehaviour
{
    public static bool inLcs = false;
    public static string path = "FCG/Textures/";
    public static string halfBodyImagePath = "Texture/UI/Common/qiuyuan/";
    public static string headImagePath = "Texture/UI/Common/playerSmallIcon/";
    public static string clubImagePath = "Texture/UI/Common/clubIcon/";
    static LinkedList<Sprite> loadedFootballerBodyPool = new LinkedList<Sprite>();
    static LinkedList<Sprite> loadedFootballerHeadPool = new LinkedList<Sprite>();
    static LinkedList<Sprite> loadedFootballerCardBg0Pool = new LinkedList<Sprite>();
    static LinkedList<Sprite> loadedFootballerCardBg1Pool = new LinkedList<Sprite>();
    static int poolSize = 300;
    static int poolCleanupSize = 50;


    //加载图片
    public static Sprite LoadSprite(string path)
    {
#if LCS
        Texture2D t = AppFacade.Instance.ResMgr.LoadTexture(path) as Texture2D;
#else
        Texture2D t = CResourceManager.Load<Texture2D>(path);
#endif
        if (t)
        {
            return Sprite.Create(t, new Rect(new Vector2(0, 0), new Vector2(t.width, t.height)), new Vector2(0, 0));
        }
        else
        {
            Debugger.LogError("图片不存在" + path);
            //return new Sprite();
            return null;
        }
    }

    public static string GetRarityImg(int i)
    {
        return path + "3DCard/Rarity" + i;
    }

    public static string GetPositionImg(int i)
    {
        return path + "3DCard/p" + i;
    }
    
    //获取位置颜色图片
    public static Sprite GetPosImg(int i)
    {
        int[] x = new int[11] { 4, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1 };
        return LoadSprite(path + "Collection/ty_wz_0" + x[i]);
    }

    //获取位置名字
    public static string GetPosName(int i)
    {
        string[] name = new string[11] { "GK", "RB", "LB", "CB", "CM", "RM", "LM", "CAM", "RW", "LW", "CF" };
        return name[i];
    }

    public static string GetClubImage(int id)
    {
        return clubImagePath + id;
    }

    //获取卡牌背景图片
    public static string GetCardBg0(int isStar)
    {
        string sp = path + "3DCard/cardBg" + isStar;
        /*loadedFootballerCardBg0Pool.AddLast(sp);
        if (loadedFootballerCardBg0Pool.Count > poolSize)
        {
            for (int i = 0; i < loadedFootballerCardBg0Pool.Count; i++)
            {
                Resources.UnloadAsset(loadedFootballerCardBg0Pool.First.Value.texture);
                loadedFootballerCardBg0Pool.First.Value = null;
                loadedFootballerCardBg0Pool.RemoveFirst();
                if (i >= poolCleanupSize)
                {
                    break;
                }
            }
        }*/
        return sp;
    }

    //获取卡牌背景图片
    public static string GetCardBg1(int isStar)
    {
        string sp = path + "3DCard/cardStar" + isStar;
        /*loadedFootballerCardBg1Pool.AddLast(sp);
        if (loadedFootballerCardBg1Pool.Count > poolSize)
        {
            for (int i = 0; i < loadedFootballerCardBg1Pool.Count; i++)
            {
                Resources.UnloadAsset(loadedFootballerCardBg1Pool.First.Value.texture);
                loadedFootballerCardBg1Pool.First.Value = null;
                loadedFootballerCardBg1Pool.RemoveFirst();
                if (i >= poolCleanupSize)
                {
                    break;
                }
            }
        }*/
        return sp;
    }

    //获取品质名称
    public static string GetRarityName(int i)
    {
        string[] name = new string[6] { "橙", "紫", "蓝", "绿", "青", "银" };
        return name[i];
    }

    //是否包含某个位置
    public static bool IsAblePosition(Footballer data, int pos)
    {
        for (int i = 0; i < data.PosLength; i++)
        {
            if (data.Pos(i) == pos)
            {
                return true;
            }
        }
        return false;
    }

    //获取卡背图片
    public static Sprite GetCardBack(int i)
    {
        return LoadSprite(path + "Match/cardback");
    }

    //获取场上单人品质图片
    public static Sprite GetMatchRarityMy(int i)
    {
        return LoadSprite(path + "3DCard/cardBlue" + i);
    }

    //获取场上双人品质图片
    public static Sprite GetMatchRarityOpp(int i)
    {
        return LoadSprite(path + "3DCard/cardRed" + i);
    }
    //获取球员头像
    public static string GetPlayerBody(int rid)
    {
        string sp = halfBodyImagePath + rid + "b";
        /*loadedFootballerBodyPool.AddLast(sp);
        if (loadedFootballerBodyPool.Count > poolSize)
        {
            for (int i = 0; i < loadedFootballerBodyPool.Count; i++)
            {
                Resources.UnloadAsset(loadedFootballerBodyPool.First.Value.texture);
                loadedFootballerBodyPool.RemoveFirst();
                if (i >= poolCleanupSize)
                {
                    break;
                }
            }
        }*/
        return sp;
    }

    //获取球员头像
    public static string GetPlayerHead(int rid)
    {
        string sp = headImagePath + rid + "s";
        /*loadedFootballerHeadPool.AddLast(sp);
        if (loadedFootballerHeadPool.Count > poolSize)
        {
            for (int i = 0; i < loadedFootballerHeadPool.Count; i++)
            {
                Resources.UnloadAsset(loadedFootballerHeadPool.First.Value.texture);
                loadedFootballerHeadPool.RemoveFirst();
                if (i >= poolCleanupSize)
                {
                    break;
                }
            }
        }*/
        return sp;
    }

    static FCGGameConfig GC = StaticDataUtil.instance.gameConfig;
    static int[] A = new int[5]{
            GC.RarityLv1GoldSoulSynthesis,
            GC.RarityLv2GoldSoulSynthesis,
            GC.RarityLv3GoldSoulSynthesis,
            GC.RarityLv4GoldSoulSynthesis,
            GC.RarityLv5GoldSoulSynthesis
        };
    static int[] B = new int[5]{
            GC.RarityLv1SoulSynthesis,
            GC.RarityLv2SoulSynthesis,
            GC.RarityLv3SoulSynthesis,
            GC.RarityLv4SoulSynthesis,
            GC.RarityLv5SoulSynthesis
        };
    static int[] C = new int[5]{
            GC.RarityLv1StarGoldSoulSynthesis,
            GC.RarityLv2StarGoldSoulSynthesis,
            GC.RarityLv3StarGoldSoulSynthesis,
            GC.RarityLv4StarGoldSoulSynthesis,
            GC.RarityLv5StarGoldSoulSynthesis
        };
    static int[] D = new int[5]{
            GC.RarityLv1StarSoulSynthesis,
            GC.RarityLv2StarSoulSynthesis,
            GC.RarityLv3StarSoulSynthesis,
            GC.RarityLv4StarSoulSynthesis,
            GC.RarityLv5StarSoulSynthesis
        };
    static int[] E = new int[6]
    {
        0,
        GC.RarityLv1CanSynthesis,
        GC.RarityLv2CanSynthesis,
        GC.RarityLv3CanSynthesis,
        GC.RarityLv4CanSynthesis,
        GC.RarityLv5CanSynthesis,
    };
    static int[] F = new int[6]
    {
        0,
        GC.RarityLv1GoldCanSynthesis,
        GC.RarityLv2GoldCanSynthesis,
        GC.RarityLv3GoldCanSynthesis,
        GC.RarityLv4GoldCanSynthesis,
        GC.RarityLv5GoldCanSynthesis,
    };

    //获取制作球员卡所需球魂
    public static int GetNeedPoint(Footballer data)
    {
        int r = data.Rarity - 1;
        if (r == -1) return 0;
        if (data.IsGolden == 1)
        {
            if (data.IsStar == 1)
            {
                return C[r];
            }
            else
            {
                return A[r];
            }
        }
        else
        {
            if (data.IsStar == 1)
            {
                return D[r];
            }
            else {
                return B[r];
            }
        }
    }

    //获取分解球员卡所得球魂
    public static float GetEarnPoint(Footballer data)
    {
        int r = data.Rarity - 1;
        if (r == -1) return 0;
        if (data.IsGolden == 1)
        {
            if (data.IsStar == 1)
            {
                return C[r] * GC.GoldStarCardResolveDepreciation;
            }
            else
            {
                return A[r] * GC.GoldCardResolveDepreciation;
            }
        }
        else
        {
            if (data.IsStar == 1)
            {
                return D[r] * GC.StarCardResolveDepreciation;
            }
            else
                return B[r] * GC.CardResolveDepreciation;
        }
    }

    public static bool CanMake(Footballer data)
    {
        int r = data.Rarity;
        if (data.IsGolden == 1)
        {
            return F[r] == 1;
        }
        else
        {
            return E[r] == 1;
        }
    }

    public static int GetTicketType(int i)
    {
        int[] ticketType = new int[2]
        {
            StaticDataUtil.instance.baseEnumConfig.CashResourceTypeTicket1,
            StaticDataUtil.instance.baseEnumConfig.CashResourceTypeTicket2
        };
        return ticketType[i];
    }

    public static int GetPriceType(int type, int index)
    {
        switch (type)
        {
            case (0):
                return GetTicketType(index);
            case (1):
                return StaticDataUtil.instance.baseEnumConfig.CashResourceTypeCash1;
            case (2):
                return StaticDataUtil.instance.baseEnumConfig.CashResourceTypeCash2;
        }
        return 0;
    }

    public static bool HaveSkill(Footballer data, int skill)
    {
        if (data.SkillLength > 1)
        {
            return data.Skill(0) == skill || data.Skill(1) == skill;
        }
        else
        {
            if (data.SkillLength > 0)
            {
                return data.Skill(0) == skill;
            }
            else
            {
                return false;
            }
        }
    }
}
